<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-left">
        <img src="@/assets/logo.png" alt="" />
        <span>电商管理后台系统</span>
      </div>

      <el-button type="info" @click="logout">退出</el-button></el-header
    >
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 伸缩侧边栏按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          :default-active="$route.path"
          router
          :collapse-transition="false"
          :collapse="isCollapse"
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
        >
          <!-- 一级菜单的模板区域 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->

            <el-menu-item
              v-for="it in item.children"
              :index="'/' + it.path"
              :key="it.id"
            >
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsLcon[it.id]"></i>
                <!-- 文本 -->
                <span>{{ it.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体结构 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      menulist: [], // 左侧菜单数据

      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iconsLcon: {
        110: 'el-icon-s-check',
        111: 'el-icon-user',
        112: 'el-icon-setting',
        104: 'el-icon-s-grid',
        115: 'el-icon-document-copy',
        121: 'el-icon-s-data',
        107: 'el-icon-edit-outling',
        146: 'el-icon-office-building'
      },
      isCollapse: false // 点击折叠
    }
  },
  created() {
    this.getMenuList()
  },
  mounted() {},
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    // 获取左侧菜单栏
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 左侧菜单栏折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  },
  computed: {},
  watch: {}
}
</script>

<style lang="scss">
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 10px;
    .header-left {
      display: flex;
      align-items: center;
      img {
        transform: scale(0.25);
      }
      span {
        color: #fff;
        font-size: 24px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
    .toggle-button {
      background-color: #4a5064;
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 22px;
      cursor: pointer;
      letter-spacing: 0.2em;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
